package com.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.dao.IUserDao;
import com.dao.entity.User;
import com.dao.redis.IRedislLoginVerifyDao;
import com.dao.redis.redisEntity.LoginVerify;
import com.net.base.BaseRequest;
import com.net.base.BaseResponse;
import com.net.filter.LoginFilter;
import com.util.Md5Utils;
import com.util.NetUtil;
import com.util.Utils;

/**
 * 用户控制器
 * 
 * @author 尚业
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
	private static Logger logger = Logger.getLogger(LoginFilter.class);

	@Autowired
	IUserDao userDao;

	@Autowired
	IRedislLoginVerifyDao redislLoginVerifyDao;

	/**
	 * 注册
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/register")
	@ResponseBody
	public BaseResponse<User> register(@RequestBody BaseRequest<User> request) {
		if (request == null) {
			logger.warn("请求数据为空");
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		User user = NetUtil.getBodyFromBaseRequest(request, User.class);
		if (user == null) {
			logger.warn("请求数据为空");
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		User userDaoSelect = userDao.selectByUserName(user.getUsername());
		if (userDaoSelect != null) {
			return NetUtil.getCommonFailuerResponse("账号已存在");
		}

		user.setPrestige(5);
		user.setMoney(0.0);
		userDao.insert(user);

		return NetUtil.getCommonSuccessResponse("注册成功");
	}
	/**
	 * 登陆
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/modify")
	@ResponseBody
	public BaseResponse<User> login(HttpServletRequest request,HttpServletResponse response, @RequestBody BaseRequest<User> requestBody) {
		if (request == null) {
			logger.warn("请求数据为空");
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String userName = Utils.getShopnameBytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		if (userName == null) {
			return NetUtil.getCommonFailuerResponse("会话过期");
		}
		User user = NetUtil.getBodyFromBaseRequest(requestBody, User.class);
		userDao.updateByPrimaryKey(user);
		
		
		return NetUtil.getCommonSuccessResponse("修改成功");
	}
	/**
	 * 登陆
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public BaseResponse<User> login(HttpSession session,
			HttpServletResponse response, @RequestBody BaseRequest<User> request) {
		if (request == null) {
			logger.warn("请求数据为空");
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}

		User user = NetUtil.getBodyFromBaseRequest(request, User.class);
		if (user == null) {
			logger.warn("请求数据为空");
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		User userDaoSelect = userDao.selectByUserName(user.getUsername());
		if (userDaoSelect == null) {
			logger.warn("账号不存在，请核对");
			return NetUtil.getCommonFailuerResponse("账号不存在，请核对");
		} else if(userDaoSelect.getIdentity()<2 && userDaoSelect.getIdentity()!=user.getIdentity()){
			logger.warn("身份信息不匹配");
			return NetUtil.getCommonFailuerResponse("身份信息不匹配");
		}
		else if (!Md5Utils.toMd5String(user.getUserpwd()).equals(userDaoSelect.getUserpwd())) {
			logger.warn("密码错误，请重新输入");
			return NetUtil.getCommonFailuerResponse("密码错误，请重新输入");
		}
		response.setHeader("JSESSIONID",session.getId());
		
		// 登录会话
		addLoginVerify(session.getId(), 0, userDaoSelect.getUsername(),userDaoSelect.getName1(),userDaoSelect.getShopname());
		logger.warn("登陆成功");
		return NetUtil.getDataResponse(userDaoSelect, "登陆成功");
	}

	private void addLoginVerify(String token, long timestamp, String userName,String name1,String shopname) {
		LoginVerify verify = new LoginVerify();
		verify.setToken(token);
		verify.setTimestamp(timestamp);
		verify.setUserName(userName);
		verify.setName1(name1);
		verify.setShopname(shopname);
		redislLoginVerifyDao.add(verify);
	}

}
